import { FormSchema } from '@/components/Form/index';
import { ref, unref } from 'vue';
import Dictionary from '@/utils/dictionary';
// import Industry from '@/utils/industry';
import dayjs, { Dayjs } from 'dayjs';
import { district, industryDownList } from '@/api/sys';
import { phoneReg, telReg } from '@/utils/Reg';
// import { IIndustry } from '@/api/sys/model/indexModel';

export function basicsInfoSchemas(): FormSchema[] {
  const dicts = Dictionary.getLocalMulti(['orgType', 'enabled']);
  const nowDate = ref(dayjs().add(1, 'year').format('YYYY-MM-DD'));
  return [
    {
      field: 'divider-basic',
      component: 'Divider',
      label: '基础设置',
      colProps: {
        span: 24,
      },
    },
    {
      field: 'orgType',
      component: 'RadioGroup',
      label: '平台类型',
      defaultValue: dicts.orgType[0]?.dictValue,
      colProps: {
        span: 20,
      },
      componentProps: {
        options: dicts.orgType.map((v) => ({ label: v.dictLabel, value: v.dictValue })),
      },
    },
    {
      field: 'orgCode',
      component: 'Input',
      label: '机构编码',
      colProps: {
        span: 10,
      },
      required: true,
      itemProps: {
        extra: '（建议使用机构名称的首字母大写，机构登录时使用）',
      },
      componentProps: {
        placeholder: '请输入机构编码',
      },
    },
    {
      field: 'website',
      component: 'Input',
      label: '网址域名',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '例：（***）.huibangtech.cn',
      },
      itemProps: {
        extra: '(仅支持http网页协议，①商协邦域名：需解析二级域名②外部域名：域名需关联商协邦服务器IP)',
      },
    },
    {
      field: 'validityTime',
      component: 'DatePicker',
      label: '有效期',
      colProps: {
        span: 10,
      },
      defaultValue: unref(nowDate.value),
      required: true,
      componentProps: {
        placeholder: '请输入机构的有效期',
        disabledDate: (current: Dayjs) => {
          return current && current < dayjs().endOf('day');
        },
      },
      itemProps: {
        extra: '（默认是一年，支持手动修改）',
      },
    },
    {
      field: 'divider-basic',
      component: 'Divider',
      label: '机构资料',
      colProps: {
        span: 24,
      },
    },
    {
      field: 'orgName',
      component: 'Input',
      label: '机构名称',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入机构名称',
      },
    },
    {
      field: 'orgAbbreviation',
      component: 'Input',
      label: '机构简称',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入机构简称',
      },
    },
    {
      field: 'industryId',
      component: 'ApiSelect',
      label: '行业类别',
      colProps: {
        span: 10,
      },
      componentProps: {
        placeholder: '请选择行业类别',
        labelField: 'name',
        valueField: 'id',
        api: industryDownList,
        // options: indus.map((v) => ({ label: v.name, value: v.id })),
      },
    },
    {
      field: 'officeTel',
      component: 'Input',
      label: '机构座机',
      // required: true,
      colProps: {
        span: 10,
      },
      componentProps: {
        placeholder: '请输入机构座机',
      },
      rules: [
        // {
        //   type: 'number',
        //   message: '座机号码必须是数字',
        // },
        {
          validator: async (_rule, value) => {
            // if (!value) return Promise.reject('请输入机构座机');
            if (value && !telReg.test(value)) {
              return Promise.reject('请输入正确的座机格式');
            }
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ],
    },
    {
      field: 'contactsName',
      component: 'Input',
      label: '联系人',
      colProps: {
        span: 10,
      },
      componentProps: {
        placeholder: '请输入联系人',
      },
    },
    {
      field: 'contactsPhone',
      component: 'Input',
      label: '联系人电话',
      // required: true,
      colProps: {
        span: 10,
      },
      componentProps: {
        placeholder: '请输入联系人电话',
      },
      rules: [
        {
          required: true,
          validator: async (_rule, value) => {
            // if (!value) return Promise.reject('请输入联系人电话');
            if (!phoneReg.test(value)) {
              return Promise.reject('请输入正确的手机号码');
            }
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ],
    },
    {
      field: 'email',
      component: 'Input',
      label: '机构邮箱',
      colProps: {
        span: 10,
      },
      componentProps: {
        placeholder: '请输入机构邮箱',
      },
      rules: [{ type: 'email' }],
    },
    {
      field: 'provinces',
      component: 'ApiCascader',
      label: '机构地址',
      colProps: {
        span: 10,
      },
      componentProps: {
        labelField: 'extName',
        valueField: 'id',
        api: district,
        initFetchParams: 0,
        isLeaf: (record) => {
          return record.id.toString().length >= 6
        }
      },
    },
    {
      field: 'address',
      component: 'ChooseAddress',
      label: '详细地址',
      colProps: {
        span: 16,
      },
      slot: 'address',
      required: true,
      componentProps: {
        placeholder: '请输入详细地址',
      },
      // componentProps: {
      //   labelField: 'extName',
      //   valueField: 'id',
      //   api: district,
      //   initFetchParams: 0,
      // },
    },
    {
      field: 'logo',
      component: 'Input',
      slot: 'logo',
      label: '机构logo',
      colProps: {
        span: 10,
      },
    },
    {
      field: 'orgPicture',
      component: 'Input',
      slot: 'logo',
      label: '机构图片',
      colProps: {
        span: 10,
      },
    },
    {
      field: 'details',
      component: 'InputTextArea',
      label: '简短介绍',
      colProps: {
        span: 16,
      },
      componentProps: {
        placeholder: '文字控制在200字以内',
        maxlength: 200,
        showCount: true,
        autoSize: { minRows: 6 },
      },
      itemProps: {
        extra: ' ',
      },
    },
  ];
}

export function wechatPlatformConfigSchemas(): FormSchema[] {
  return [
    {
      field: 'divider-basic',
      component: 'Divider',
      label: '公众号设置',
      colProps: {
        span: 24,
      },
    },
    {
      field: 'wxName',
      component: 'Input',
      label: '名称',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入公众号名称',
      },
    },
    {
      field: 'wxType',
      component: 'Select',
      label: '类型',
      defaultValue: 0,
      colProps: {
        span: 10,
      },
      componentProps: {
        allowClear: false,
        options: [
          {
            label: '服务号',
            value: 0,
          },
          {
            label: '订阅号',
            value: 1,
          },
        ],
      },
    },
    {
      field: 'wxAccount',
      component: 'Input',
      label: '账号',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入公众号账号',
      },
    },
    {
      field: 'wxPassword',
      component: 'InputPassword',
      label: '密码',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入公众号密码',
        autocomplete: 'autocomplete',
      },
    },
    {
      field: 'wxId',
      component: 'Input',
      label: '原始ID',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入公众号的原始ID',
      },
    },
    {
      field: 'wxAppId',
      component: 'Input',
      label: '应用APPID',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入公众号的APPID',
      },
    },
    {
      field: 'wxSecret',
      component: 'Input',
      label: 'APPSecret',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入公众号的APPSecret',
      },
    },
    {
      field: 'wxToken',
      component: 'Input',
      label: 'Token',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入公众号的Token',
      },
    },
    {
      field: 'wxDockingAddress',
      component: 'Input',
      label: '对接地址',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入对接地址',
      },
    },
    {
      field: 'wxVerifyFile',
      component: 'Upload',
      label: '验证文件',
      colProps: {
        span: 10,
      },
      componentProps: {
        api: async () => { },
      },
      required: true,
    },
    {
      field: 'divider-basic',
      component: 'Divider',
      label: '小程序设置',
      colProps: {
        span: 24,
      },
    },
    {
      field: 'wxMinName',
      component: 'Input',
      label: '名称',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入小程序名称',
      },
    },
    {
      field: 'wxMinAccount',
      component: 'Input',
      label: '账号',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入小程序账号',
      },
    },
    {
      field: 'wxMinPassword',
      component: 'Input',
      label: '密码',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入小程序密码',
        // autocomplete: 'autocomplete',
      },
    },
    {
      field: 'wxMinAppId',
      component: 'Input',
      label: '应用APPID',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入小程序的APPID',
      },
    },
    {
      field: 'wxMinSecret',
      component: 'Input',
      label: 'APPSecret',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入小程序的APPSecret',
      },
    },
  ];
}

export function SMSPlatformConfigSchemas(): FormSchema[] {
  return [
    {
      field: 'smsSign',
      component: 'Input',
      label: '短信平台签名',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入短信平台签名',
      },
    },
    {
      field: 'smsAccount',
      component: 'Input',
      label: '短信平台账号',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入短信平台账号',
      },
    },
    {
      field: 'smsPassword',
      component: 'InputPassword',
      label: '短信平台密码',
      colProps: {
        span: 10,
      },
      required: true,
      componentProps: {
        placeholder: '请输入短信平台密码',
        autocomplete: 'autocomplete',
      },
    },
  ];
}

export function databaseConfigSchemas(): FormSchema[] {
  return [
    // {
    //   field: 'deploymentWay',
    //   component: 'Select',
    //   label: '部署方式',
    //   defaultValue: 0,
    //   colProps: {
    //     span: 10,
    //   },
    //   componentProps: {
    //     allowClear: false,
    //     options: [
    //       {
    //         label: 'Saas 部署',
    //         value: 0,
    //       },
    //       {
    //         label: '本地部署',
    //         value: 1,
    //       },
    //     ],
    //   },
    //   itemProps: {
    //     extra: '（默认为SAAS部署，当对方数据库要求部署在本地时，则为本地部署模式）',
    //   },
    // },
    {
      field: 'databaseIp',
      component: 'Input',
      label: '数据库IP',
      colProps: {
        span: 18,
      },
      required: true,
      componentProps: {
        placeholder: '请输入数据库IP',
      },
    },
    {
      field: 'databaseAccount',
      component: 'Input',
      label: '数据库账号',
      colProps: {
        span: 18,
      },
      required: true,
      componentProps: {
        placeholder: '请输入数据库账号',
      },
    },
    {
      field: 'databasePassword',
      component: 'InputPassword',
      label: '数据库密码',
      colProps: {
        span: 18,
      },
      required: true,
      componentProps: {
        placeholder: '请输入数据库密码',
        autocomplete: 'autocomplete',
      },
    },
    {
      field: 'databasePort',
      component: 'Input',
      label: '数据库端口',
      colProps: {
        span: 18,
      },
      required: true,
      componentProps: {
        placeholder: '请输入数据库端口',
      },
    },
  ];
}
